//
//  LJTopView.swift
//  LJWeibo
//
//  Created by Raymond on 15/12/22.
//  Copyright © 2015年 liu. All rights reserved.
//

import UIKit
import SDWebImage

class LJTopView: UIView {
    /// MARK: - 属性
    var status: LJStatus?{
        didSet{
            if let profile_image_url = status?.user?.profile_image_url{
                // 设置头像
                let url = NSURL(string: profile_image_url)
                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"))
            }
            // 设置用户名
            nameLabel.text = status?.user?.screen_name
            
            // TODO: 设置时间
            tiemLabel.text = "刚刚"
            
            // TODO: 设置来源
            sourceLabel.text = "火星"
            
            // 设置Vip
            vipView.image = status?.user?.mbrankImage
            
            // 设置认证图标
            verifiedView.image = status?.user?.varifiedImage
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
//        self.backgroundColor = UIColor.orangeColor()
        
        // 准备UI
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    /// 准备UI的方法
    func prepareUI() {
        // 添加控件
        addSubview(separatorView)
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(tiemLabel)
        addSubview(sourceLabel)
        addSubview(vipView)
        addSubview(verifiedView)
        
        // 添加约束
        
        separatorView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: self, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 8))
        
        iconView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: separatorView, size: CGSize(width: 35, height: 35), offset: CGPoint(x: 8, y: 8))
        nameLabel.ff_AlignHorizontal(type: ff_AlignType.TopRight, referView: iconView, size: nil, offset: CGPoint(x: 8, y: 0))
        tiemLabel.ff_AlignHorizontal(type: ff_AlignType.BottomRight, referView: iconView, size: nil, offset: CGPoint(x: 8, y: 0))
        sourceLabel.ff_AlignHorizontal(type: ff_AlignType.CenterRight, referView: tiemLabel, size: nil, offset: CGPoint(x: 8, y: 0))
        vipView.ff_AlignHorizontal(type: ff_AlignType.CenterRight, referView: nameLabel, size: CGSize(width: 14, height: 14), offset: CGPoint(x: 8, y: 0))
        verifiedView.ff_AlignInner(type: ff_AlignType.BottomRight, referView: iconView, size: CGSize(width: 17, height: 17), offset: CGPoint(x: 7.5, y: 7.5))
    }
    
    /// MARK: - 懒加载
    /// cell分割视图
    private lazy var separatorView: UIView = {
       let view = UIView()
        view.backgroundColor = UIColor(white: 0.9, alpha: 1)
        return view
    }()
    /// 用户头像
    lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar"))
    
    /// 用户名称
    lazy var nameLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 14)
    
    /// 微博时间
    lazy var tiemLabel: UILabel = UILabel(textColor: UIColor.orangeColor(), fontSize: 9)
    
    /// 微博来源
    lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.lightGrayColor(), fontSize: 9)
    
    /// 会员等级
    lazy var vipView: UIImageView = UIImageView(image: UIImage(named: "avatar"))
    
    /// 认证图标
    lazy var verifiedView: UIImageView = UIImageView(image: UIImage(named: "avatar"))

}
